package homework6;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class Container implements Collection{
	
	private static final int DEFAULT_INITIAL_CAPACITY = 1<<4;
	private static final int MAXIMUM_CAPACITY = 1<<30;
	private static final float DEFAULT_LOAD_FACTOR = 0.75F;
	private static final Object UNUSED = new Object();
	
	private Object[]elements;
	private int counter;
	private float loadFactor;
	
	public Container(int initialCapacity,float loadFactor) {
		if(initialCapacity<0 || initialCapacity>MAXIMUM_CAPACITY) {
			RuntimeException ex = new RuntimeException("初始容量必须是 [ 0 , " + MAXIMUM_CAPACITY + " ] 之间的正整数值");
			throw ex;
		}
		if(loadFactor<=0 || loadFactor>=1) {
			RuntimeException ex = new RuntimeException("加载因子必须是 ( 0 , 1 ) 之间的浮点数");
			throw ex;
		}
		elements = new Object[initialCapacity];
		this.loadFactor=loadFactor;
		this.mark();
	}
	public Container(int initialCapacity) {
		this(initialCapacity,DEFAULT_LOAD_FACTOR);
	}
	public Container() {
		this(DEFAULT_INITIAL_CAPACITY,DEFAULT_LOAD_FACTOR);
	}
	
	private void mark() {
		Arrays.fill(elements, UNUSED);
	}

	@Override
	public int size() {		
		return this.counter;	
	}

	@Override
	public boolean isEmpty() {
		return this.counter==0;
	}	

	@Override
	public Object[] toArray() {
		Object[] array=Arrays.copyOf(elements, counter);
		return array;
	}
	//扩容操作
	private void ensureCapacity() {
		if(counter>elements.length*loadFactor) {
			final Object[] temp = this.elements;
			int newLength=temp.length*3/2+1;
			if(newLength>MAXIMUM_CAPACITY) {
				RuntimeException x = new RuntimeException( "容器容量超过最大值" );
				throw x ;
			}
			this.elements = new Object[newLength];
			System.arraycopy(temp, 0, elements, 0, counter);
		}
	}
	
	@Override
	public boolean add(Object e) {
		this.ensureCapacity();
		elements[counter]=e;
		counter++;
		return true;
	}
	
	public int indexOf(Object o) {
		for( int i = 0 ; i < counter ; i++ ) {
			Object e = elements[ i ]; 
			if( e == o || ( e != null && e.equals( o ) ) ) {
				return i ;  
			}
		}
		return -1 ;
	}
	
	@Override
	public boolean contains(Object o) {
		return this.indexOf(o)!=-1;
	}
	
	@Override
	public boolean remove(Object o) {
		int index=this.indexOf(o);
		if(index != -1) {
			int n = counter-1-index;
			System.arraycopy(elements, index+1, elements, index, n);
			elements[counter-1]=UNUSED;
			counter--;
			return true;
		}
		return false;
	}

	@Override
	public boolean containsAll(Collection c) {
		Object[] array = c.toArray();
		boolean flag = true;
		if(counter >= array.length) {			
			for(int i =0 ; i < array.length; i++) {
				if(!contains(array[i])) {
					flag = false;
					break;
				}
			}			
		}	
		return flag;
	}

	@Override
	public boolean addAll(Collection c) {
		Object[] array = c.toArray();
		if(elements != null) {
			for(int i=0;i<array.length;i++) {
				this.add(array[i]);
			}
			return true;
		}
		return false;				
	}

	@Override
	public boolean removeAll(Collection c) {
		Object[] array = c.toArray();
		if(elements != null && counter > 0) {
			
			for(int i = 0 ;i < array.length;i++) {
				for(int j = 0;j < counter ;j++) {
					
					if(array[i].equals(elements[j])) {
						this.remove(elements[j]);
						System.out.println("*"+this.toString());
					}
					
				}
			}
			return true;
		}
		return false;
	}

	@Override
	public boolean retainAll(Collection c) {
		Object[] array = c.toArray();
		if(elements != null && counter > 0 && array != null && array.length != 0) {
			
			for(int i = 0; i < counter ;i++) {
				boolean flag = false;
				for(int j = 0;j < array.length;j++) {
					if(array[j].equals(elements[i])) {
						flag = true;
					}
				}
				if(!flag) {
					this.remove(elements[i]);
				}
			}
			return true;
		}
		return false;
	}

	@Override
	public void clear() {
		this.counter=0;
		this.mark();
		
	}
	
	private final StringBuilder builder = new StringBuilder();
	
	@Override
	public String toString() {
		builder.append( "[" );
		if (counter > 0) {
			for (int i = 0; i < counter; i++) {
				// 将每个元素的字符串形式追加到 builder 末尾
				builder.append( elements[i] ); 
				builder.append( i < counter - 1 ? "," : "" );
			}
		}
		builder.append( "]" );
		String s = builder.toString();
		// 将 builder 的 length 设置为 零 是为了下次使用 builder 时，从缓冲区的 第一个位置开始存放字符
		builder.setLength( 0 ); // 功能上类似于容器的 clear 方法
		return s;
	}
	
	@Override
	public Object[] toArray(Object[] a) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Iterator iterator() {
		// TODO Auto-generated method stub
		return null;
	}
}